home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicButtonUI.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  283 lines

  1. /*
  2.  * @(#)BasicButtonUI.java    1.80 98/06/22
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package com.sun.java.swing.plaf.basic;
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.io.Serializable;
  20. import com.sun.java.swing.*;
  21. import com.sun.java.swing.border.*;
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import com.sun.java.swing.plaf.ButtonUI;
  25. import com.sun.java.swing.plaf.UIResource;
  26. import com.sun.java.swing.plaf.ComponentUI;
  27.  
  28. /**
  29.  * BasicButton implementation
  30.  * <p>
  31.  * Warning: serialized objects of this class will not be compatible with
  32.  * future swing releases.  The current serialization support is appropriate 
  33.  * for short term storage or RMI between Swing1.0 applications.  It will
  34.  * not be possible to load serialized Swing1.0 objects with future releases
  35.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  36.  * baseline for the serialized form of Swing objects.
  37.  *
  38.  * @version 1.80 06/22/98
  39.  * @author Jeff Dinkins
  40.  */
  41. public class BasicButtonUI extends ButtonUI implements Serializable {
  42.  
  43.     protected Color getSelectColor()       { return UIManager.getColor("Button.pressed"); }
  44.     protected Color getDisabledColor()     { return UIManager.getColor("Button.disabled"); }
  45.     protected Color getDisabledTextColor() { return UIManager.getColor("Button.disabledText"); }
  46.     protected Color getFocusColor()        { return UIManager.getColor("Button.focus"); }
  47.  
  48.     // Borders
  49.       private final static Insets defaultMargin = new Insets(2,14,2,14);
  50.  
  51.     // Visual constants
  52.     private static final int defaultTextIconGap = 4;
  53.   
  54.   // offset controlled by set method 
  55.     private int shift_offset = 0;
  56.  
  57.     // Shared UI object
  58.     protected static ButtonUI buttonUI;
  59.  
  60.     public static ComponentUI createUI(JComponent c) {
  61.     if(buttonUI == null) {
  62.             buttonUI = new BasicButtonUI();
  63.     }
  64.         return buttonUI;
  65.     }
  66.  
  67.     protected BasicButtonListener listener;
  68.  
  69.     public void installUI(JComponent c) {
  70.       installDefaults((AbstractButton) c);
  71.       installListeners((AbstractButton) c);
  72.       installKeyboardActions((AbstractButton) c);
  73.     }
  74.  
  75.     protected void installDefaults(AbstractButton b) {
  76.       b.setOpaque(false); // exterior "is Default" border turns on and off 
  77.       LookAndFeel.installColorsAndFont(b, "Button.background", 
  78.                       "Button.foreground", 
  79.                       "Button.font");
  80.       LookAndFeel.installBorder(b,"Button.border");
  81.     }
  82.  
  83.     protected void installListeners(AbstractButton b) {
  84.     if (listener == null) {
  85.         listener = createListener(b);
  86.     }
  87.     b.addMouseListener(listener);
  88.     b.addMouseMotionListener(listener);
  89.     b.addFocusListener(listener);
  90.         b.addPropertyChangeListener(listener);
  91.     b.addChangeListener(listener);
  92.     }
  93.     
  94.     protected void installKeyboardActions(AbstractButton b){
  95.     listener.installKeyboardActions(b);
  96.     }
  97.  
  98.     
  99.     public void uninstallUI(JComponent c) {
  100.         uninstallKeyboardActions((AbstractButton) c);
  101.         uninstallListeners((AbstractButton) c);
  102.         uninstallDefaults((AbstractButton) c);
  103.     }
  104.  
  105.     protected void uninstallKeyboardActions(AbstractButton b) {
  106.     listener.uninstallKeyboardActions(b);
  107.     }
  108.  
  109.     protected void uninstallListeners(AbstractButton b) {
  110.         b.removeMouseListener(listener);
  111.         b.removeMouseListener(listener);
  112.         b.removeMouseMotionListener(listener);
  113.         b.removeFocusListener(listener);
  114.         b.removeChangeListener(listener);
  115.         b.removePropertyChangeListener(listener);
  116.     }
  117.  
  118.     protected void uninstallDefaults(AbstractButton b) {
  119.         LookAndFeel.uninstallBorder(b);
  120.     }
  121.   
  122.     protected BasicButtonListener createListener(AbstractButton b) {
  123.     return new BasicButtonListener(b);
  124.     }
  125.  
  126.  
  127.     /**
  128.      * ComponentUI implementation for BasicButton
  129.      *
  130.      */
  131.     public void paint(Graphics g, JComponent c) {
  132.         AbstractButton b = (AbstractButton) c;
  133.         ButtonModel model = b.getModel();
  134.  
  135.         Dimension size = b.getSize();
  136.         FontMetrics fm = g.getFontMetrics();
  137.  
  138.     Insets i = c.getInsets();
  139.  
  140.         Rectangle viewRect = new Rectangle(size);
  141.  
  142.     viewRect.x += i.left;
  143.     viewRect.y += i.top;
  144.     viewRect.width -= (i.right + viewRect.x);
  145.     viewRect.height -= (i.bottom + viewRect.y);
  146.  
  147.         Rectangle iconRect = new Rectangle(); 
  148.     Rectangle textRect = new Rectangle();
  149.  
  150.     Font f = c.getFont();
  151.     g.setFont(f);
  152.  
  153.     // layout the text and icon
  154.         String text = SwingUtilities.layoutCompoundLabel(
  155.         fm, b.getText(), b.getIcon(),
  156.         b.getVerticalAlignment(), b.getHorizontalAlignment(),
  157.         b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  158.         viewRect, iconRect, textRect, b.getText() == null ? 0 : defaultTextIconGap
  159.     );
  160.  
  161.     setTextShiftOffset(0);
  162.  
  163.     // perform UI specific press action, ie
  164.     // ie. Basic L&F shifts text
  165.         if (model.isArmed() && model.isPressed()) {
  166.       paintButtonPressed(g,b); 
  167.         }
  168.  
  169.     // Paint the Icon
  170.         if(b.getIcon() != null) { 
  171.         paintIcon(g,c,iconRect);
  172.         }
  173.  
  174.     if (text != null && !text.equals("")){
  175.         paintText(g, c,textRect, text);
  176.     }
  177.  
  178.     if (b.isFocusPainted() && b.hasFocus()) {
  179.       // paint UI specific focus
  180.       paintFocus(g,b,viewRect,textRect,iconRect);
  181.     }
  182.     }
  183.  
  184.     protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){
  185.             AbstractButton b = (AbstractButton) c;                 
  186.             ButtonModel model = b.getModel();
  187.             Icon icon = null;
  188.             if(!model.isEnabled()) {
  189.                 icon = (Icon) b.getDisabledIcon();
  190.             } else if(model.isPressed() && model.isArmed()) {
  191.                 icon = (Icon) b.getPressedIcon();
  192.                 if(icon == null) {
  193.                     // Use default icon
  194.                     icon = (Icon) b.getIcon();
  195.                 } else {
  196.             // revert back to 0 offset
  197.           setTextShiftOffset(0);
  198.             }
  199.             } else if(b.isRolloverEnabled() && model.isRollover()) {
  200.                 icon = (Icon) b.getRolloverIcon();
  201.             }
  202.           
  203.         if (icon == null) {
  204.                 icon = (Icon) b.getIcon();
  205.         }
  206.            
  207.             if(model.isPressed() && model.isArmed()) {
  208.                 icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
  209.             iconRect.y + getTextShiftOffset());
  210.             } else {
  211.                 icon.paintIcon(c, g, iconRect.x, iconRect.y);
  212.             }
  213.  
  214.     }
  215.  
  216.     protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  217.     AbstractButton b = (AbstractButton) c;                 
  218.     ButtonModel model = b.getModel();
  219.     FontMetrics fm = g.getFontMetrics();
  220.  
  221.     /* Draw the Text */
  222.     if(model.isEnabled()) {
  223.         /*** paint the text normally */
  224.         g.setColor(b.getForeground());
  225.         BasicGraphicsUtils.drawString(g,text, model.getMnemonic(),
  226.                       textRect.x + getTextShiftOffset(),
  227.                       textRect.y + fm.getAscent() + getTextShiftOffset());
  228.     }
  229.     else {
  230.         /*** paint the text disabled ***/
  231.         g.setColor(b.getBackground().brighter());
  232.         BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  233.                       textRect.x, textRect.y + fm.getAscent());
  234.         g.setColor(b.getBackground().darker());
  235.         BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  236.                       textRect.x - 1, textRect.y + fm.getAscent() - 1);
  237.     }
  238.     }
  239.     
  240.  
  241.     protected void paintFocus(Graphics g, AbstractButton b,
  242.                   Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  243.     }
  244.   
  245.     protected void paintButtonPressed(Graphics g, AbstractButton b){
  246.     }
  247.  
  248.     protected void setTextShiftOffset(int i){
  249.     this.shift_offset = i;
  250.     }
  251.  
  252.     private int getTextShiftOffset(){
  253.     return this.shift_offset;
  254.     }
  255.  
  256.     public Dimension getMinimumSize(JComponent c) {
  257.         return getPreferredSize(c);
  258.     }
  259.  
  260.     public Dimension getPreferredSize(JComponent c) {
  261.     AbstractButton b = (AbstractButton)c;
  262.     return BasicGraphicsUtils.getPreferredButtonSize(b, defaultTextIconGap);
  263.     }
  264.  
  265.     public Dimension getMaximumSize(JComponent c) {
  266.         return getPreferredSize(c);
  267.     }
  268.  
  269.     /**
  270.      * insets and margin
  271.      */
  272.  
  273.     public Insets getDefaultMargin(AbstractButton b) {
  274.     return defaultMargin;
  275.     }
  276.  
  277.     public int getDefaultTextIconGap(AbstractButton b) {
  278.     return defaultTextIconGap;
  279.     }
  280.  
  281. }
  282.  
  283.